<?php

namespace Common\Model;
use think\Model;

class UserHourlistModel extends KuerpWarehouseModel{


	//追加属性
    public $append = [
        //'staff_info',         //员工信息
	];

    //员工信息
    function getStaffInfoAttr($val, &$data){
        $model = M('Staff');
        $wdata['stId'] = $data['stId'];
        $vo = $model->field('*')->where($wdata)->find();
        $data['hour_price'] = $vo['hour_price'];
        $data['total'] = floatval($vo['hour_price']*$data['hour']);
        return $vo;
    }

    function _before_search(){
        $map = [];
        $map['is_del'] = 0;
        if(I('date_id')){
            $csd_model = M('CaiwuSalaryDate');
            $csd_wdata['id'] = I('date_id');
            $csd_wdata['type'] = 1;
            $csd_vo = $csd_model->where($csd_wdata)->find();
            if(!$csd_vo){
                $map['no_execute'] = true;//不执行搜索
                //$this->ajaxErrReturn('日期数据不存在');
            }else{
                //$firstday = date('Y-m-01', strtotime($csd_vo['account_date']));
                //$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
                //$map['add_time'] = array('between',[$firstday,$lastday]);
                $map['dates'] = $csd_vo['account_date'];
            }
        }
        if(I('keyword')){
            $map['uh.stName|desct'] = array('like','%'.I('keyword').'%');
        }
        //dump($map);exit;
        return $map;
    }


    //列表
    public function _list($model, $map, $listRows='' ,$sortBy ='' , $field='*') {
        //排序字段 默认为主键名
        if (isset($_REQUEST ['_order'])) {
            $order = $_REQUEST ['_order'];
        } else {
            $order = !empty($sortBy) ? $sortBy : $model->getPk();
        }
        //排序方式默认按照倒序排列
        //接受 sost参数 0 表示倒序 非0都 表示正序
        if (isset($_REQUEST ['_sort'])) {
            $sort = $_REQUEST ['_sort'] ? 'asc' : 'desc';
        } else {
            $sort = $asc ? 'asc' : 'desc';
        }
        //取得满足条件的记录数
        $count = $this->alias('uh')->join('__STAFF__ s on uh.stId=s.stId','LEFT')->where($map)->count();
        //echo M('')->getlastsql();exit;
        if (!empty($_REQUEST ['listRows'])) {
            $listRows = $_REQUEST ['listRows'];
        } else {
            $page_size = C('page_size');
            $listRows = $page_size ? $page_size : 20;
        }
        $page_count = ceil($count / $listRows);
        if ($count > 0) {
            //创建分页对象
            $p = new \Think\AdminPage($count, $listRows);
            //分页查询数据
            
            $voList = $model->alias('uh')->join('__STAFF__ s on uh.stId=s.stId','LEFT')->field('uh.*,s.hour_price')->where($map)->order("`" . $order . "` " . $sort)->limit($p->firstRow . ',' . $p->listRows)->select();
            foreach($voList as &$vo){
                $vo['total'] = floatval($vo['hour_price']*$vo['hour']);
            }
            //分页跳转的时候保证查询条件
            foreach ($map as $key => $val) {
                if (!is_array($val)) {
                    $p->parameter .= "$key=" . urlencode($val) . "&";
                }
            }

            //分页显示
            $page = $p->show();

            //列表排序显示
            $sortImg = $sort; //排序图标
            $sortAlt = $sort == 'desc' ? '升序排列' : '倒序排列'; //排序提示
            $sort = $sort == 'desc' ? 1 : 0; //排序方式
            //模板赋值显示
        }else{
			$p = new \Think\AdminPage($count, $listRows);
			$voList = [];
			$page = $p->show();
		}
        $data['list'] = $voList;
		$data['page_count'] = $p->totalPages;//总页数
		$data['count'] = $p->totalRows;//总条数
        $data['listRows'] = $p->listRows;//总条数
		$data['now_page'] = $p->nowPage;
        return $data;
    }


}